package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.BigDecimalPropertyEditor;
import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.model.SystemFile;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.LinkmanService;
import com.feeyin.radar.crm.service.SaleRecordService;
import com.feeyin.radar.crm.service.SystemFileService;
import com.feeyin.radar.sys.service.DepartmentService;
import com.feeyin.radar.sys.service.RecordRangeService;
import com.feeyin.radar.sys.service.SaleStageSetService;
import com.feeyin.radar.sys.service.UserService;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class SaleRecordController extends MultiActionController
{
  private SaleRecordService saleRecordService;
  private CustomerService customerService;
  private SaleStageSetService saleStageSetService;
  private RecordRangeService recordRangeService;
  private LinkmanService linkmanService;
  private SystemFileService systemFileService;
  private UserService userService;
  private DepartmentService departmentService;
  private String saleRecord;
  private String addSaleRecord;
  private String viewSaleRecord;
  private String viewSaleRecordInfo;
  private String selectCustomer;
  private String selectLinkman;
  private String addSaleRecordOne;
  private String updateSaleRecord;
  private String updateSaleRecordInfo;
  private String advancedQuery;
  private String deptSaleRecord;
  private String addSaleRecordHangup;
  private String addSaleRecordCare;
  private String addSaleRecordClose;

  public UserService getUserService()
  {
    return this.userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public DepartmentService getDepartmentService() {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public SaleRecordService getSaleRecordService() {
    return this.saleRecordService;
  }

  public void setSaleRecordService(SaleRecordService saleRecordService) {
    this.saleRecordService = saleRecordService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public SaleStageSetService getSaleStageSetService() {
    return this.saleStageSetService;
  }

  public void setSaleStageSetService(SaleStageSetService saleStageSetService) {
    this.saleStageSetService = saleStageSetService;
  }

  public RecordRangeService getRecordRangeService() {
    return this.recordRangeService;
  }

  public void setRecordRangeService(RecordRangeService recordRangeService) {
    this.recordRangeService = recordRangeService;
  }

  public LinkmanService getLinkmanService() {
    return this.linkmanService;
  }

  public void setLinkmanService(LinkmanService linkmanService) {
    this.linkmanService = linkmanService;
  }

  public SystemFileService getSystemFileService() {
    return this.systemFileService;
  }

  public void setSystemFileService(SystemFileService systemFileService) {
    this.systemFileService = systemFileService;
  }

  public String getSaleRecord() {
    return this.saleRecord;
  }

  public void setSaleRecord(String saleRecord) {
    this.saleRecord = saleRecord;
  }

  public String getAddSaleRecord() {
    return this.addSaleRecord;
  }

  public void setAddSaleRecord(String addSaleRecord) {
    this.addSaleRecord = addSaleRecord;
  }

  public String getViewSaleRecord() {
    return this.viewSaleRecord;
  }

  public void setViewSaleRecord(String viewSaleRecord) {
    this.viewSaleRecord = viewSaleRecord;
  }

  public String getViewSaleRecordInfo() {
    return this.viewSaleRecordInfo;
  }

  public void setViewSaleRecordInfo(String viewSaleRecordInfo) {
    this.viewSaleRecordInfo = viewSaleRecordInfo;
  }

  public String getSelectCustomer() {
    return this.selectCustomer;
  }

  public void setSelectCustomer(String selectCustomer) {
    this.selectCustomer = selectCustomer;
  }

  public String getSelectLinkman() {
    return this.selectLinkman;
  }

  public void setSelectLinkman(String selectLinkman) {
    this.selectLinkman = selectLinkman;
  }

  public String getAddSaleRecordOne() {
    return this.addSaleRecordOne;
  }

  public void setAddSaleRecordOne(String addSaleRecordOne) {
    this.addSaleRecordOne = addSaleRecordOne;
  }

  public String getUpdateSaleRecord() {
    return this.updateSaleRecord;
  }

  public void setUpdateSaleRecord(String updateSaleRecord) {
    this.updateSaleRecord = updateSaleRecord;
  }

  public String getUpdateSaleRecordInfo() {
    return this.updateSaleRecordInfo;
  }

  public void setUpdateSaleRecordInfo(String updateSaleRecordInfo) {
    this.updateSaleRecordInfo = updateSaleRecordInfo;
  }

  public String getAdvancedQuery() {
    return this.advancedQuery;
  }

  public void setAdvancedQuery(String advancedQuery) {
    this.advancedQuery = advancedQuery;
  }

  public String getDeptSaleRecord() {
    return this.deptSaleRecord;
  }

  public void setDeptSaleRecord(String deptSaleRecord) {
    this.deptSaleRecord = deptSaleRecord;
  }

  public String getAddSaleRecordHangup() {
    return this.addSaleRecordHangup;
  }

  public void setAddSaleRecordHangup(String addSaleRecordHangup) {
    this.addSaleRecordHangup = addSaleRecordHangup;
  }

  public String getAddSaleRecordCare() {
    return this.addSaleRecordCare;
  }

  public void setAddSaleRecordCare(String addSaleRecordCare) {
    this.addSaleRecordCare = addSaleRecordCare;
  }

  public String getAddSaleRecordClose() {
    return this.addSaleRecordClose;
  }

  public void setAddSaleRecordClose(String addSaleRecordClose) {
    this.addSaleRecordClose = addSaleRecordClose;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response, QueryCustomer queryCustomer)
  {
    Map map = new HashMap();
    map.put("isNotUpdate", request.getParameter("isNotUpdate"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String customerName = "";
    String sFlag = request.getParameter("sFlag");
    map.put("sFlag", sFlag);
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    map.put("customerName", customerName);

    QueryCustomer qCustomer = null;
    if ((!StringUtils.isEmpty(sFlag)) && ("2".equals(sFlag))) {
      qCustomer = queryCustomer;
    }
    map.put("queryCustomer", qCustomer);

    Page page = new Page();
    page.getPageList(this.saleRecordService.getPageMySaleRecordCustomersCount(customerName, userId, qCustomer).intValue(), null, request);
    map.put("customers", this.saleRecordService.getPageMySaleRecordCustomers(customerName, userId, page, qCustomer));

    return new ModelAndView(getSaleRecord(), map);
  }

  public ModelAndView deptSaleRecord(HttpServletRequest request, HttpServletResponse response, QueryCustomer queryCustomer)
  {
    Map model = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String customerName = "";
    String sFlag = request.getParameter("sFlag");
    model.put("sFlag", sFlag);
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    model.put("customerName", customerName);

    QueryCustomer qCustomer = null;
    if ((!StringUtils.isEmpty(sFlag)) && ("2".equals(sFlag))) {
      qCustomer = queryCustomer;
    }
    model.put("queryCustomer", qCustomer);
    Page page = new Page();
    page.getPageList(this.saleRecordService.getPageDeptSaleRecordCustomersCount(customerName, userId, queryCustomer).intValue(), null, request);
    model.put("customers", this.saleRecordService.getPageDeptSaleRecordCustomers(customerName, userId, page, queryCustomer));

    return new ModelAndView(getDeptSaleRecord(), model);
  }

  public ModelAndView advancedQuery(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("records", this.customerService.getCustomerInfoRecords());
    model.put("ranges", this.customerService.getCustomerDictionaryRanges());
    model.put("saleStages", this.saleStageSetService.getSaleStages());
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    List users = new ArrayList();
    if (userId != null) {
      String deptIds = this.departmentService.getCurrentDeptIdAndSubDeptIdsByUserId(userId);
      users = this.userService.getActivityUsersByDeptIds(deptIds);
    }
    model.put("users", users);
    return new ModelAndView(getAdvancedQuery(), model);
  }

  public ModelAndView selectCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    Page page = new Page();
    page.getPageList(this.customerService.getPageMyAllCustomersCount("0", userId).intValue(), Integer.valueOf(10), request);
    map.put("customers", this.customerService.getPageMyAllCustomers("0", userId, page));

    return new ModelAndView(getSelectCustomer(), map);
  }

  public ModelAndView selectLinkman(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    String customerId = request.getParameter("customerId");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("linkmansAndDepts", this.linkmanService.getLinkmanAndDepts(Integer.valueOf(Integer.parseInt(customerId))));
    }
    return new ModelAndView(getSelectLinkman(), map);
  }

  public ModelAndView preAddSaleRecord(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("saleStages", this.saleStageSetService.getSaleStages());
    map.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));

    return new ModelAndView(getAddSaleRecord(), map);
  }

  public ModelAndView addSaleRecord(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    String sign = request.getParameter("sign");
    MultipartFile multipartFile = saleRecord.getContents();
    if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
      (multipartFile.getSize() > 10000000L)) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("saleRecord", saleRecord);
      return preAddSaleRecord(request, response);
    }

    String realPath = "";
    if ((multipartFile != null) && (!multipartFile.isEmpty())) {
      realPath = getServletContext().getRealPath("/");
    }
    boolean result = this.saleRecordService.insertSaleRecord(saleRecord, realPath, multipartFile);
    int success = 0;
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./saleRecord.do?action=preAddSaleRecord&result=" + success + "&sign=" + sign));
  }

  public ModelAndView preAddSaleRecordOne(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("saleStages", this.saleStageSetService.getSaleStages());
    map.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));

    String customerId = request.getParameter("customerId");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleRecord", this.saleRecordService.getSaleRecordCustomerInfoByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
    }

    return new ModelAndView(getAddSaleRecordOne(), map);
  }

  public ModelAndView addSaleRecordOne(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    String sign = request.getParameter("sign");
    MultipartFile multipartFile = saleRecord.getContents();
    if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
      (multipartFile.getSize() > 10000000L)) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("saleRecord", saleRecord);
      return preAddSaleRecordOne(request, response);
    }

    String realPath = "";
    if ((multipartFile != null) && (!multipartFile.isEmpty())) {
      realPath = getServletContext().getRealPath("/");
    }
    boolean result = this.saleRecordService.insertSaleRecord(saleRecord, realPath, multipartFile);
    int success = 0;
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./saleRecord.do?action=preAddSaleRecordOne&result=" + success + "&customerId=" + saleRecord.getCustomerId() + "&sign=" + sign));
  }

  public ModelAndView preUpdateSaleRecord(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("saleStages", this.saleStageSetService.getSaleStages());
    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);

    List saleRecords = new ArrayList();
    saleRecords = this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 1, "0");
    if ((saleRecords == null) || (saleRecords.size() <= 0)) {
      return new ModelAndView(new RedirectView("./saleRecord.do?action=display&isNotUpdate=1"));
    }
    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleRecords", saleRecords);
    }
    return new ModelAndView(getUpdateSaleRecord(), map);
  }

  public ModelAndView preUpdateSaleRecordInfo(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("deleteResult", request.getParameter("deleteResult"));
    map.put("uploadResult", request.getAttribute("uploadResult"));
    String realPath = getServletContext().getRealPath("/");

    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    String saleRecordId = request.getParameter("id");

    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleStages", this.saleStageSetService.getSaleStages());
      map.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));

      if (!StringUtils.isEmpty(saleRecordId)) {
        map.put("saleRecord", this.saleRecordService.getSaleRecordByCustomerIdAndSaleRecordId(realPath, Integer.valueOf(Integer.parseInt(customerId)), Integer.valueOf(Integer.parseInt(saleRecordId))));
      }
    }
    return new ModelAndView(getUpdateSaleRecordInfo(), map);
  }

  public ModelAndView updateSaleRecordInfo(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    String sign = request.getParameter("sign");
    boolean result = false;
    int success = 0;
    if ((saleRecord != null) && (saleRecord.getId() != null)) {
      MultipartFile multipartFile = saleRecord.getContents();
      if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
        (multipartFile.getSize() > 10000000L)) {
        request.setAttribute("uploadResult", Integer.valueOf(3));
        request.setAttribute("saleRecord", saleRecord);
        return preUpdateSaleRecordInfo(request, response);
      }

      String realPath = "";
      if ((multipartFile != null) && (!multipartFile.isEmpty()) && (saleRecord != null)) {
        realPath = getServletContext().getRealPath("/");
        boolean isFileExists = false;
        isFileExists = this.saleRecordService.isFileExists(realPath, null, saleRecord.getCustomerId(), saleRecord.getId(), multipartFile.getOriginalFilename());
        if (isFileExists) {
          request.setAttribute("uploadResult", Integer.valueOf(4));
          request.setAttribute("saleRecord", saleRecord);
          return preUpdateSaleRecordInfo(request, response);
        }
      }
      result = this.saleRecordService.updateSaleRecord(saleRecord, realPath, multipartFile);
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./saleRecord.do?action=preUpdateSaleRecordInfo&result=" + success + "&customerId=" + saleRecord.getCustomerId() + "&id=" + saleRecord.getId() + "&sign=" + sign));
  }

  public ModelAndView deleteAdded(HttpServletRequest request, HttpServletResponse response)
  {
    String customerId = request.getParameter("customerId");
    String saleRecordId = request.getParameter("id");
    String addedId = request.getParameter("addedId");

    String realPath = getServletContext().getRealPath("/");
    boolean result = false;
    int success = 0;
    if (!StringUtils.isEmpty(addedId)) {
      result = this.saleRecordService.deleteAdded(realPath, Integer.valueOf(Integer.parseInt(addedId)));
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./saleRecord.do?action=preUpdateSaleRecordInfo&customerId=" + customerId + "&id=" + saleRecordId + "&deleteResult=" + success));
  }

  public ModelAndView viewSaleRecord(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("saleStages", this.saleStageSetService.getSaleStages());
    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "0"));
      map.put("hisRootSaleRecords", this.saleRecordService.getCloseSaleRcordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
      map.put("hisSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "1"));
      map.put("careSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "2"));
      map.put("hangupSaleRecords", this.saleRecordService.getSaleRecordsByCustomerId(Integer.valueOf(Integer.parseInt(customerId)), 0, "3"));
      map.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
    }
    return new ModelAndView(getViewSaleRecord(), map);
  }

  public ModelAndView viewSaleRecordInfo(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    String realPath = getServletContext().getRealPath("/");

    String customerId = request.getParameter("customerId");
    map.put("customerId", customerId);
    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleStages", this.saleStageSetService.getSaleStages());
      map.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));
      if (!StringUtils.isEmpty(id)) {
        map.put("saleRecord", this.saleRecordService.getSaleRecordByCustomerIdAndSaleRecordId(realPath, Integer.valueOf(Integer.parseInt(customerId)), Integer.valueOf(Integer.parseInt(id))));
      }
    }

    return new ModelAndView(getViewSaleRecordInfo(), map);
  }

  public ModelAndView preAddSaleRecordHangup(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("sign", request.getParameter("sign"));
    String customerId = request.getParameter("customerId");
    model.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));
    if (!StringUtils.isEmpty(customerId)) {
      model.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
    }
    return new ModelAndView(getAddSaleRecordHangup(), model);
  }

  public ModelAndView addSaleRecordHangup(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    MultipartFile multipartFile = saleRecord.getContents();
    if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
      (multipartFile.getSize() > 10000000L)) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("saleRecord", saleRecord);
      return preAddSaleRecordHangup(request, response);
    }

    String realPath = "";
    if ((multipartFile != null) && (!multipartFile.isEmpty())) {
      realPath = getServletContext().getRealPath("/");
    }
    boolean result = this.saleRecordService.insertSaleRecordHangup(saleRecord, realPath, multipartFile);
    int success = 0;
    if (result) success = 2;

    String sign = request.getParameter("sign");
    if ((!StringUtils.isEmpty(sign)) && ("2".equals(sign)))
    {
      return new ModelAndView(new RedirectView("./saleRecord.do?action=display&deleteResult=" + success));
    }

    return new ModelAndView(new RedirectView("./customerManage.do?action=display&deleteResult=" + success));
  }

  public ModelAndView preAddSaleRecordCare(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    String customerId = request.getParameter("customerId");
    model.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));
    if (!StringUtils.isEmpty(customerId)) {
      model.put("customer", this.customerService.getCustomerById(Integer.valueOf(Integer.parseInt(customerId))));
    }
    return new ModelAndView(getAddSaleRecordCare(), model);
  }

  public ModelAndView addSaleRecordCare(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    MultipartFile multipartFile = saleRecord.getContents();
    if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
      (multipartFile.getSize() > 10000000L)) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("saleRecord", saleRecord);
      return preAddSaleRecordCare(request, response);
    }

    String realPath = "";
    if ((multipartFile != null) && (!multipartFile.isEmpty())) {
      realPath = getServletContext().getRealPath("/");
    }
    boolean result = this.saleRecordService.insertSaleRecordCare(saleRecord, realPath, multipartFile);
    int success = 0;
    if (result) success = 2;

    return new ModelAndView(new RedirectView("./contractCustomer.do?action=display&deleteResult=" + success));
  }

  public ModelAndView preAddSaleRecordClose(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("sign", request.getParameter("sign"));
    map.put("result", request.getParameter("result"));
    map.put("saleStages", this.saleStageSetService.getSaleStages());
    map.put("contactWays", this.recordRangeService.getActivityRecordRangesByDictionaryCode("34"));

    String customerId = request.getParameter("customerId");
    if (!StringUtils.isEmpty(customerId)) {
      map.put("saleRecord", this.saleRecordService.getSaleRecordCustomerInfoByCustomerId(Integer.valueOf(Integer.parseInt(customerId))));
    }

    return new ModelAndView(getAddSaleRecordClose(), map);
  }

  public ModelAndView addSaleRecordClose(HttpServletRequest request, HttpServletResponse response, SaleRecord saleRecord) throws Exception
  {
    MultipartFile multipartFile = saleRecord.getContents();
    if ((multipartFile != null) && (!multipartFile.isEmpty()) && 
      (multipartFile.getSize() > 10000000L)) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("saleRecord", saleRecord);
      return preAddSaleRecordOne(request, response);
    }

    String realPath = "";
    if ((multipartFile != null) && (!multipartFile.isEmpty())) {
      realPath = getServletContext().getRealPath("/");
    }
    boolean result = this.saleRecordService.insertSaleRecordClose(saleRecord, realPath, multipartFile);
    int success = 0;
    if (result) success = 3;

    String sign = request.getParameter("sign");
    if ((!StringUtils.isEmpty(sign)) && ("2".equals(sign)))
    {
      return new ModelAndView(new RedirectView("./saleRecord.do?action=display&deleteResult=" + success));
    }

    return new ModelAndView(new RedirectView("./customerManage.do?action=display&deleteResult=" + success));
  }

  public ModelAndView download(HttpServletRequest request, HttpServletResponse response)
  {
    String customerId = request.getParameter("customerId");
    String saleRecordId = request.getParameter("id");
    String addedId = request.getParameter("added");

    String realPath = getServletContext().getRealPath("/");

    if ((!StringUtils.isEmpty(saleRecordId)) && 
      (!StringUtils.isEmpty(customerId)) && 
      (!StringUtils.isEmpty(addedId)) && 
      (!StringUtils.isEmpty(realPath))) {
      SystemFile systemFile = new SystemFile();
      systemFile = this.systemFileService.getSystemFile(Integer.valueOf(Integer.parseInt(addedId)));
      if (systemFile != null) {
        boolean isFileExists = false;
        isFileExists = this.saleRecordService.isFileExists(null, realPath + systemFile.getPath(), Integer.valueOf(Integer.parseInt(customerId)), Integer.valueOf(Integer.parseInt(saleRecordId)), systemFile.getFileName());
        if (isFileExists) {
          String path = realPath + systemFile.getPath() + systemFile.getFileName();
          try
          {
            File file = new File(path);

            String filename = file.getName();

            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            response.reset();

            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO8859_1"));
            response.addHeader("Content-Length", file.length()+"");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
          } catch (IOException ex) {
            ex.printStackTrace();
          }
        }
      }
    }
    return null;
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
    throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());

    binder.registerCustomEditor(BigDecimal.class, new BigDecimalPropertyEditor());
  }
}